package com.ljt.yupictureddd.infrastracture.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.ljt.yupictureddd.infrastracture.common.BaseResponse;
import com.ljt.yupictureddd.infrastracture.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 接受satoken的未登录异常
     */
    @ExceptionHandler(NotLoginException.class)
    public BaseResponse notLoginExceptionHandler(NotLoginException e) {
        log.error("NotLoginException:",e);
        return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,e.getMessage());
    }
    /**
     * 接受satoken的没权限异常
     */
    @ExceptionHandler(NotPermissionException.class)
    public BaseResponse notLoginExceptionHandler(NotPermissionException e) {
        log.error("NotPermissionException:",e);
        return ResultUtils.error(ErrorCode.NO_AUTH_ERROR,e.getMessage());
    }
    /**
     * 业务异常的处理
     * @param e
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e) {
        log.error("businessException:",e);
        return ResultUtils.error(e.getCode(),e.getMessage());
    }

    /**
     * 除了业务异常要处理，前端应该返回系统错误，后端应该在日志看到错误详情（这是我们看的懂的）
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public BaseResponse businessExceptionHandler(Exception e) {
        log.error("businessException:",e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"系统错误");
    }
}
